package com.rear.common.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import com.rear.inter.base.JsonVo;
import com.rear.inter.base.ValidateException;

/**
 * @author 所有action的父类
 * 
 */
@Controller
public class ManageBaseAction {

    protected final Logger logger = Logger.getLogger(this.getClass());

   

    /**
     * 参数校验
     * 
     * @param json
     *            json数据Bean
     * @throws ValidateException
     */
    protected <T> void validate(JsonVo<T> json) throws ValidateException {
        if (json.getErrors().size() > 0) {
            json.setResult(false);
            throw new ValidateException("有错误发生");
        } else {
            json.setResult(true);
        }
    }


    /**
     * 远程jsop请求基础服务 将处理好的json数据进行封装返回
     * 
     * @param jsonDate
     *            要返回客户端的json数据
     */
    protected String ajaxJsonResponse(HttpServletResponse response, HttpServletRequest request, String jsonDate) {

        try {

            String callback = request.getParameter("callback");
            String returnResult = "";
            returnResult = jsonDate;
            response.setContentType("text/json;charset=utf-8");
            response.addHeader("Access-Control-Allow-Origin", "*");
            if ("IE".equals(request.getParameter("type"))) {
                response.addHeader("XDomainRequestAllowed", "1");
            }

            response.getWriter().write(returnResult);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }
}
